// 用于存放detail的数据
import Taro from '@tarojs/taro';
import moment from 'moment'
import { ISchedule, WeekDayEnum, getDevice, weekMap } from './tool'

let detail: ISchedule | undefined

function getDefaultAddSchedule(): ISchedule {
  const now = moment();
  return {
    enabled: true,
    dateObj: now,
    repeatArray: new Set<WeekDayEnum>(),
    timeObj: { hour: now.hour(), minute: now.minute() },
    did: getDevice()!.did,
  }
}

export function goToDetialPage({ schedule }: { schedule?: ISchedule }) {
  if (schedule) {
    detail = schedule
  } else {
    detail = getDefaultAddSchedule()
  }
  detail.enabled = true
  Taro.navigateTo({ url: '/pages/scheduler/Detail/index' })
}

export function updateWeek(weeks: Set<WeekDayEnum>) {
  if (detail) {
    const sortedArray = Array.from(weeks).sort((a, b) => weekMap[a] - weekMap[b]);
    detail.repeatArray = new Set(sortedArray)
  }
}

export function getDetail() {
  return detail
}

export default detail